package com.itheima.demo;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrudDemo {
    //设置全局变量
    private SqlSession session = null;

    //在@Test之前执行
    @Before
    public void before() throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
    }

    //在@Test之后执行
    @After
    public void after() {
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
    }

    //查询所有用户信息
    @Test
    public void selectAll() {
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.selectAll");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //根据id查询
    @Test
    public void selectById(){
        //4、调用具体的statement对象，执行sql
        User user = session.selectOne("UserMapper.selectById", 2);
        //处理结果
        System.out.println(user);
    }

    //添加用户信息
    @Test
    public void insertUser() {
        User user = new User();
        user.setUname("李武");
        user.setUpwd("123");
        //4、调用具体的statement对象，执行sql
        session.insert("UserMapper.insertUser",user);//更新操作一定要有提交事务
    }

    //修改用户信息
    @Test
    public void updateUser() {
        User user = session.selectOne("UserMapper.selectById", 3);
        user.setUname("李月");
        user.setUpwd("333");
        //4、调用具体的statement对象，执行sql
        session.update("UserMapper.updateUser",user);//更新操作一定要有提交事务
    }

    //删除用户信息
    @Test
    public void deleteUser() {
        //4、调用具体的statement对象，执行sql
        session.delete("UserMapper.deleteUser",7);//更新操作一定要有提交事务
    }

    //统计人数
    @Test
    public void total(){
        //4、调用具体的statement对象，执行sql
        int u = session.selectOne("UserMapper.total");
        //处理结果
        System.out.println(u);
    }

    //模糊查询1
    @Test
    public void findlikeName1(){
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName","%李%");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //模糊查询2 推荐使用
    @Test
    public void findlikeName2(){
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName2","李");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //多个参数传递实现
    //登录：用户名和密码
    @Test
    public void findByNameAndPwd() {
        //创建map对象，存放多个参数：name和pwd
        Map map = new HashMap();
        map.put("uname","张三");
        map.put("upwd","123");
        User user = session.selectOne("UserMapper.findByNameAndPwd", map);
        System.out.println(user);
    }

    //输出结果类型的map
    //统计男女人数
    @Test
    public void findSex() {
        List<Map<String,Object>> list = session.selectList("UserMapper.findSex");
        Map<String,Long> map = new HashMap<String, Long>();
        for (Map<String, Object> mp : list) {
            map.put((String)mp.get("sex"),(Long)mp.get("total"));
        }
        System.out.println(map);
    }
}
